/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.server;

import java.util.List;
import java.util.logging.Logger;

import de.uhilger.netzpult.client.PublicService;
import de.uhilger.netzpult.server.db.DataStorage;
import de.uhilger.netzpult.shared.Document;
import de.uhilger.netzpult.shared.Folder;
import de.uhilger.netzpult.shared.UserShare;
/*
 * TODO f�r alle Methoden noch die Pr�fung auf den �ffentlich-Ordner einbauen: Jeder Aufruf darf nur Inhalte betreffen, die im �ffentlich-Ordner liegen. 
 */
/**
 * In der Klasse PublicServiceImpl sind alle Methoden enthalten, die auf der 
 * Serverseite ablaufen und keine besonderen Berechtigungen erfordern. 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class PublicServiceImpl extends AbstractWebfilesServlet implements PublicService {

	private static final long serialVersionUID = -4634208437256962690L;
	
	@Override
	public List<Folder> getChildren(String owner, Folder folder) throws Exception {
		return ds.getChildren(folder, owner);
	}

	@Override
	public List<Document> getDocuments(String owner, Folder folder) throws Exception {
		return ds.getDocuments(owner, folder, DataStorage.WITHOUT_DRAFT);
	}

	@Override
	public Document getDocument(String owner, String idStr) throws Exception {
		try {
			int id = Integer.parseInt(idStr);
			//String docOwner = ds.getDocumentOwner(id);
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("docId=" + idStr + 
					/*"docOwner=" + docOwner + */ ", owner=" + owner);
			//if(docOwner.equals(owner)) { 
			if(ds.isPermitted(owner, id, UserShare.READ)) {
				Document document = ds.getDocument(id, DataStorage.WITHOUT_DRAFT, DataStorage.WITHOUT_CONTENT);
				String mimeType = document.getMimeType();
				if(mimeType.equalsIgnoreCase("application/zip") || 
						mimeType.equalsIgnoreCase("application/octet-stream") ||
						mimeType.equalsIgnoreCase("application/x-zip-compressed")) {
					return document;
				} else {
					return ds.getDocument(id, DataStorage.WITHOUT_DRAFT, DataStorage.WITH_CONTENT);
				}
			} else {
				//throw new Exception("User does not own document");
				throw new Exception("Documetn is not public");
			}
		} catch(Exception ex) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).severe(ex.getMessage());
			throw ex;
		}
	}
	
	public List<Document> findDocuments(String owner, String searchItem) throws Exception {
		return ds.findDocuments(owner, searchItem);
	}

	@Override
	public List<Folder> getRootFolders(String owner) throws Exception {
		return ds.getPublicRootFolders(owner);
	}

	@Override
	public String getDefaultOwner() throws Exception {
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("defaultOwner=" + defaultOwner);
		return defaultOwner;
	}
	

	@Override
	public List<Folder> getFoldersContaining(String owner, int documentId) throws Exception {
		try {
			//String docOwner = ds.getDocumentOwner(documentId);
			//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("docId=" + documentId + 
			//		"docOwner=" + docOwner + ", owner=" + owner);
			//if(docOwner.equals(owner)) {
			if(ds.isPermitted(owner, documentId, UserShare.READ)) {
				return ds.getFoldersContaining(documentId);
			} else {
				throw new Exception("User not permitted for document");
			}
		} catch(Exception ex) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).severe(ex.getMessage());
			throw ex;
		}
	}

	@Override
	public List<Folder> getPathToFolder(Folder folder) throws Exception {
		return ds.getFolderPath(folder);
	}

	@Override
	public Document getDocumentForPath(String owner, String path) throws Exception {
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("path=" + path);
		return getDocument("/" + owner + path);
	}

	@Override
	public String getDocumentOwner(int docId) throws Exception {
		return ds.getDocumentOwner(docId);
	}

	@Override
	public String getDefaultDocument() throws Exception {
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest("defaultDocumentId=" + defaultDocumentId);
		return defaultDocumentId;
	}
}
